﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Jermaine.UI
{
    /// <summary>
    /// WaitLayer
    /// </summary>
    public class WaitLayer : System.Windows.Forms.Form
    {
        const int GWL_EXSTYLE = -20;

        const int WS_EX_TRANSPARENT = 0x20;

        const int WS_EX_LAYERED = 0x80000;

        const int LWA_ALPHA = 2;

        private Control target;

        private byte alpha = 128;

        public WaitLayer(Control target)
            : base() {
            this.InitializeComponent();
            this.Load += WaitLayerLoad;
            this.target = target;

        }

        public WaitLayer(Control target, byte alpha)
            : this(target) {
            this.alpha = alpha;

        }


        private void WaitLayerLoad(object sender, EventArgs e) {
            this.BackColor = Color.Silver;
            this.ShowIcon = false;
            
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.Manual;
            this.AdjustLayout();
            var parent = GetParentForm(target);
            if (parent != null) {
                parent.TopMost = true;
                parent.Enabled = false;
            }
            this.TopMost = true;
            NativeMethods.SetWindowLong(Handle, GWL_EXSTYLE,
                (int)(NativeMethods.GetWindowLong(Handle, GWL_EXSTYLE).ToInt32() |
                WS_EX_TRANSPARENT |
                WS_EX_LAYERED));
            NativeMethods.SetLayeredWindowAttributes(Handle, 0, alpha, LWA_ALPHA); ;
        }

        private Form GetParentForm(Control c) {
            var parent = c;
            while (parent != null) {
                if (parent is Form) {
                    return parent as Form;
                }
                parent = parent.Parent;
            }
            return null;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
            var parent = GetParentForm(target);
            if (parent != null) {
                parent.TopMost = false;
                parent.Enabled = true;
            }
            base.OnClosing(e);
        }

        private void AdjustLayout() {
            this.Left = target.Left;
            this.Top = target.Top;
            this.Width = target.Width;
            this.Height = target.Height;
        }

        protected override void SetVisibleCore(bool value) {
            var parent = GetParentForm(target);
            if (parent != null) {
                parent.TopMost = value;
                parent.Enabled = !value;
            }
            base.SetVisibleCore(value);
        }

        private void InitializeComponent() {
            this.SuspendLayout();
            // 
            // WaitLayer
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "WaitLayer";
            this.ResumeLayout(false);

        }


    }
}
